package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.morningstar.common.constants.BizConstants;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.system.dao.entity.SysMessage;
import com.morningstar.system.service.SysMessageService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import static com.morningstar.common.constants.permission.PermissionConstants.DELETE;
import static com.morningstar.common.constants.permission.PermissionConstants.LIST;

/**
 * 系统消息 接口
 *
 * @author Admin
 */
@Slf4j
@RestController
@RequestMapping("/system/message")
public class SysMessageController {

    private static final String PERMISSION = "system.message";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_READ = PERMISSION + ".read";
    private static final String PERMISSION_READ_ALL = PERMISSION + ".read.all";
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;

    @Resource
    private SysMessageService messageService;

    /**
     * 我的消息列表
     *
     * @param commonQuery 通用查询类
     * @return 我的消息列表
     */
    @PostMapping("/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysMessage>> list(@RequestBody CommonQuery<SysMessage, SysMessage> commonQuery) {
        SysMessage query = commonQuery.getQuery();
        LambdaQueryWrapper<SysMessage> queryWrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            queryWrapper.like(StringUtils.isNotBlank(query.getMessageContent()), SysMessage::getMessageContent, query.getMessageContent());
            queryWrapper.like(StringUtils.isNotBlank(query.getMessageTitle()), SysMessage::getMessageTitle, query.getMessageTitle());
            queryWrapper.eq(StringUtils.isNotBlank(query.getStatus()), SysMessage::getStatus, query.getStatus());
        }
        queryWrapper.eq(SysMessage::getUserId, StpUtil.getLoginIdAsLong());
        queryWrapper.orderByDesc(SysMessage::getCreateTime);
        queryWrapper.orderByDesc(SysMessage::getMessageId);
        return Result.successData(messageService.page(commonQuery.getPage(), queryWrapper));
    }

    /**
     * 首页查询未读消息
     *
     * @return 首页查询未读消息
     */
    @PostMapping("/unReadMessage")
    @SaCheckPermission(PERMISSION_READ)
    public Result<IPage<SysMessage>> unReadMessage() {
        LambdaQueryWrapper<SysMessage> queryWrapper = new LambdaQueryWrapper<SysMessage>()
                .eq(SysMessage::getUserId, StpUtil.getLoginIdAsLong())
                .eq(SysMessage::getStatus, BizConstants.NO)
                .orderByDesc(SysMessage::getCreateTime)
                .orderByDesc(SysMessage::getMessageId);
        Page<SysMessage> page = new Page<>();
        page.setCurrent(1);
        page.setSize(5);
        return Result.successData(messageService.page(page, queryWrapper));
    }

    /**
     * 标记消息为已读
     *
     * @param messageId 消息Id
     * @return 基本结果类
     */
    @GetMapping("/read")
    @SaCheckPermission(PERMISSION_READ)
    public Result<Object> read(@RequestParam("messageId") Long messageId) {
        LambdaUpdateWrapper<SysMessage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysMessage::getMessageId, messageId);
        updateWrapper.set(SysMessage::getStatus, BizConstants.YES);
        messageService.update(updateWrapper);
        return Result.successNoData();
    }

    /**
     * 标记所有消息为已读
     *
     * @return 基本结果类
     */
    @GetMapping("/readAll")
    @SaCheckPermission(PERMISSION_READ_ALL)
    public Result<Object> readAll() {
        LambdaUpdateWrapper<SysMessage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysMessage::getUserId, StpUtil.getLoginIdAsLong());
        updateWrapper.set(SysMessage::getStatus, BizConstants.YES);
        messageService.update(updateWrapper);
        return Result.successNoData();
    }

    /**
     * 删除消息
     *
     * @param messageId 消息Id
     * @return 基本结果类
     */
    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("messageId") Long messageId) {
        messageService.removeById(messageId);
        return Result.successNoData();
    }
}
